<template>
  <layout-content
    v-loading="loading"
    :header="$t('commons.button.create')"
    :back-to="{ name: 'ClusterRoleBindings' }"
  >
    <div>
      <el-row :gutter="20">
        <div v-if="!showYaml">
          <el-form ref="form" label-position="top" :model="form" :rules="rules">
            <el-col :span="6">
              <el-form-item
                :label="$t('commons.table.name')"
                required
                prop="metadata.name"
              >
                <el-input v-model="form.metadata.name" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-tabs
                v-model="activeName"
                tab-position="top"
                type="border-card"
              >
                <el-tab-pane :label="$t('business.access_control.authorized')">
                  <ko-role-object
                    v-model:role-ref-obj="form.roleRef"
                    v-model:subject-array="form.subjects"
                    :cluster-name="cluster"
                  />
                </el-tab-pane>
                <el-tab-pane
                  :label="$t('business.workload.labels_annotations')"
                >
                  <ko-key-value
                    v-model:value="form.metadata.labels"
                    :title="$t('business.workload.label')"
                  />
                  <ko-key-value
                    v-model:value="form.metadata.annotations"
                    :title="$t('business.workload.annotations')"
                  />
                </el-tab-pane>
              </el-tabs>
            </el-col>
          </el-form>
        </div>
        <div v-if="showYaml">
          <yaml-editor ref="yaml_editor" :value="form" />
        </div>
      </el-row>
    </div>
    <div class="bottom-button">
      <el-button @click="onCancel()">{{
        $t("commons.button.cancel")
      }}</el-button>
      <el-button v-if="!showYaml" @click="onEditYaml()">{{
        $t("commons.button.yaml")
      }}</el-button>
      <el-button v-if="showYaml" @click="backToForm()">{{
        $t("commons.button.back_form")
      }}</el-button>
      <el-button v-loading="loading" type="primary" @click="onSubmit">
        {{ $t("commons.button.submit") }}
      </el-button>
    </div>
  </layout-content>
</template>

<script>
import LayoutContent from "@/views/domain/kubeHud/views/components/layout/LayoutContent.vue";
import YamlEditor from "@/components/yaml-editor";
import { createClusterRoleBinding } from "@/api/clusterrolebindings";
import Rule from "@/utils/rules";
import KoRoleObject from "@/components/ko-rbac/role-object";
import KoKeyValue from "@/components/ko-configuration/ko-key-value";

export default {
  name: "ClusterRoleBindingCreate",
  components: { YamlEditor, LayoutContent, KoRoleObject, KoKeyValue },
  data() {
    return {
      loading: false,
      cluster: "",
      form: {
        apiVersion: "rbac.authorization.k8s.io/v1",
        kind: "ClusterRoleBinding",
        metadata: {
          name: ""
        }
      },
      showYaml: false,
      rules: {
        metadata: {
          name: [Rule.RequiredRule]
        }
      },
      activeName: "",
      yaml: undefined
    };
  },
  created() {
    this.cluster = this.$route.query.cluster;
    this.showYaml = this.$route.query.yamlShow === "true";
  },
  methods: {
    onSubmit() {
      if (this.showYaml) {
        this.onCreate(this.$refs.yaml_editor.getValue());
      } else {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.onCreate(this.transformYaml());
          }
        });
      }
    },
    onCreate(data) {
      this.loading = true;
      createClusterRoleBinding(this.cluster, data)
        .then(() => {
          this.$message({
            type: "success",
            message: this.$t("commons.msg.create_success")
          });
          this.$router.push({ name: "ClusterRoleBindings" });
        })
        .finally(() => {
          this.loading = false;
        });
    },
    onCancel() {
      this.$router.push({ name: "ClusterRoleBindings" });
    },
    onEditYaml() {
      this.showYaml = true;
      this.yaml = this.transformYaml();
    },
    backToForm() {
      this.$confirm(
        this.$t("commons.confirm_message.back_form"),
        this.$t("commons.message_box.prompt"),
        {
          confirmButtonText: this.$t("commons.button.confirm"),
          cancelButtonText: this.$t("commons.button.cancel"),
          type: "warning"
        }
      ).then(() => {
        this.showYaml = false;
      });
    },
    transformYaml() {
      if (this.form?.subjects && this.form?.subjects?.length === 0) {
        delete this.form.subjects;
      }
      return JSON.parse(JSON.stringify(this.form));
    }
  }
};
</script>

<style scoped></style>
